
Region = {
		city: true,
		district: true,
		provinceCode: null,
		cityCode: null,
		
		// 初始化
		init: function(city, district, provinceCode, cityCode, districtCode) {
			this.city = city;
			this.district = district;
			this.provinceCode = provinceCode;
			this.cityCode = cityCode;
			this.districtCode = districtCode;
			this.loadProvince();
		},
		// 加载省份
		loadProvince: function(){
			
			$.getJSON(baseURL+'region/provinces/', function(data){
				  var html = '<option value="">-省份-</option>';
				  $.each(data.provinces, function(key, item){
					  if(Region.provinceCode == item.provCode) {
						  html += '<option selected value="' + item.provCode + '">' + item.provName + '</option>';
					  } else {
						  html += '<option value="' + item.provCode + '">' + item.provName + '</option>';
					  }
				  });
				  $('select[name="province"]').html(html);
				  $('#province').selectpicker('refresh');
				  if(Region.provinceCode != null) {
					  Region.loadCity();
				  }
			});
		},
		// 加载城市
		loadCity: function() {
			
			var code = $('select[name="province"]').val();
			$.getJSON(baseURL+'region/citys/'+code, function(data){
				  
				  var html = '<option value="">-城市-</option>';
				  $.each(data.citys, function(key, item){
					  if(Region.cityCode == item.cityCode) {
						  html += '<option selected value="' + item.cityCode + '">' + item.cityName + '</option>';
					  } else {
						  html += '<option value="' + item.cityCode + '">' + item.cityName + '</option>';
					  }
					  
				  });
				  $('select[name="city"]').html(html);
				  $('#city').selectpicker('refresh');
				  if(Region.cityCode != null) {
					  Region.loadDistrict();
				  }
			});
		},
		// 加载县区
		loadDistrict: function() {
			
			var code = $('select[name="city"]').val();
			$.getJSON(baseURL+'region/district/'+code, function(data){
				  
				  var html = '<option value="">-区/县-</option>';
				  $.each(data.districts, function(key, item){
					  if(Region.districtCode == item.code) {
						  html += '<option selected value="' + item.code + '">' + item.name + '</option>';
					  } else {
						  html += '<option value="' + item.code + '">' + item.name + '</option>';
					  }
				  });
				  $('select[name="district"]').html(html);
				  $('#district').selectpicker('refresh');
			});
		}
		
}
/****** select事件监听 up ******/
$(function() {
$("#province").change(function(){
	
	if (Region.city) {
		if (this.selectedIndex > 0) {
			Region.loadCity();
		} else{
			$('select[name="city"]').html('<option value="">-城市-</option>');
			$('#city').selectpicker('refresh');
		}
		if (Region.district) {
			$('select[name="district"]').html('<option value="">-区/县-</option>');
			$('#district').selectpicker('refresh');
		}
	}
})
$("#city").change(function(){
	
	if (Region.district) {
		if (this.selectedIndex > 0) {
			Region.loadDistrict();
		} else{
			if (Region.district) {
				$('select[name="district"]').html('<option value="">-区/县-</option>');
				$('#district').selectpicker('refresh');
			}
		}
	}
})
});
/****** select事件监听 end ******/
/*(function($) {
	
	var methods = {
			init: function(options){
				var settings = $.extend({
		    	      'city': true,
		    	      'district': true,
		    	      'select': false
		    	}, options);
				var $this = this;
				return $.getJSON(baseURL+'region/provinces/', function(data){
					  
					  var html = '';
					  $.each(data.provinces, function(key, item){
						  html += '<option value="' + item.provCode + '">' + item.provName + '</option>';
					  });
					  $('select[name="province"]').append(html);
					  $('#province').selectpicker('refresh');
					  // 绑定事件
					  $(window).bind('change.region', methods.loadCity);
				});
			},
			loadCity: function() {
				
				var code = $('select[name="province"]').val();
				return $.getJSON(baseURL+'region/citys/'+code, function(data){
					  
					  var html = '';
					  $.each(data.provinces, function(key, item){
						  html += '<option value="' + item.cityCode + '">' + item.cityName + '</option>';
					  });
					  $('select[name="city"]').append(html);
					  $('#city').selectpicker('refresh');
					  // 绑定事件
					  $(window).bind('change.region', methods.loadCity);
				});
			}
	}
    $.fn.region = function(method) {

		if(methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('您要调用的方法： ' +  method + ' 不存在');
		}
    	
    	
    };
})(jQuery);*/